package com.snack.shop.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.snack.shop.common.Result;
import com.snack.shop.service.FileManageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件管理控制器
 */
@Tag(name = "文件管理")
@RestController
@RequestMapping("/file/manage")
@RequiredArgsConstructor
public class FileManageController {

    private final FileManageService fileManageService;

    @Operation(summary = "确认文件（将临时文件转为正式）")
    @PostMapping("/confirm")
    public Result<String> confirmFiles(@RequestBody Map<String, Object> params) {
        @SuppressWarnings("unchecked")
        List<String> fileUrls = (List<String>) params.get("fileUrls");
        String refType = (String) params.get("refType");
        Long refId = params.get("refId") != null ? Long.valueOf(params.get("refId").toString()) : null;
        
        fileManageService.confirmFiles(fileUrls, refType, refId);
        return Result.success("确认成功");
    }

    @Operation(summary = "标记文件为已删除")
    @PostMapping("/delete")
    public Result<String> markDeleted(@RequestBody Map<String, Object> params) {
        @SuppressWarnings("unchecked")
        List<String> fileUrls = (List<String>) params.get("fileUrls");
        
        fileManageService.markDeleted(fileUrls);
        return Result.success("删除成功");
    }

    @Operation(summary = "清理临时文件（管理员）")
    @SaCheckRole("merchant")
    @PostMapping("/clean/temporary")
    public Result<Map<String, Object>> cleanTemporaryFiles(
            @RequestParam(defaultValue = "7") int days) {
        int count = fileManageService.cleanTemporaryFiles(days);
        Map<String, Object> result = new HashMap<>();
        result.put("message", "清理完成");
        result.put("deletedCount", count);
        result.put("days", days);
        return Result.success(result);
    }

    @Operation(summary = "清理已删除文件（管理员）")
    @SaCheckRole("merchant")
    @PostMapping("/clean/deleted")
    public Result<Map<String, Object>> cleanDeletedFiles(
            @RequestParam(defaultValue = "30") int days) {
        int count = fileManageService.cleanDeletedFiles(days);
        Map<String, Object> result = new HashMap<>();
        result.put("message", "清理完成");
        result.put("deletedCount", count);
        result.put("days", days);
        return Result.success(result);
    }
}

